<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const obj = {
            uname: 'pink',
            age: 18,
            hobby: ['篮球', '羽毛球'],
            family: {
                hobby: '王老师'
            }
        }
        const o = {}
        // 拷贝函数  
        function deepCopy(newObj, oldObj) {

            // debugger 断点调试

            for (const key in oldObj) {
                // 遍历数组
                //验证数据类型instanceof
                if (oldObj[key] instanceof Array) {
                    newObj[key] = []
                    //newObj[key] 接收[] hobby
                    //oldObj[k] ['篮球', '羽毛球']
                    deepCopy(newObj[key], oldObj[key])
                } else if (oldObj[key] instanceof Object) {
                    newObj[key] = {}
                    //newObj[key] 接收{} family
                    //oldObj[k] ['篮球', '羽毛球']
                    deepCopy(newObj[key], oldObj[key])
                }
                else {
                    //    key属性名  oldObj[k] 属性值
                    // newObj[key] === o.uname
                    newObj[key] = oldObj[key]
                }
            }
        }

        deepCopy(o, obj)
        console.log(o);
        o.age = 20
        o.hobby[0] = '乒乓球'
        o.family.hobby = '小夏'
        console.log(obj);

    </script>
</body>

</html>